//
//  DYZCarSourceController.m
//  DYZCars
//
//  Created by MF on 16/8/18.
//  Copyright © 2016年 MF. All rights reserved.
//

#import "DYZCarSourceController.h"
#import "DYZSearchView.h"
#import "DYZNormalCellTableViewCell.h"
#import "DYZCarSourceCell.h"
#import "DYZLaunchCarsController.h"
#import "DYZExistingCarController.h"
#import "DYZCarModel.h"
#import "DYZHighCarModel.h"
#import "DYZNetworkManager.h"
#import "DYZHighCarModel.h"
#import "DYZCarSourceDetailController.h"
#import "DYZCarSeriesController.h"
static NSString *normalCellId = @"normalCellId";
static NSString *carSourceCellId = @"carSourceCellId";
@interface DYZCarSourceController ()<UITableViewDataSource,UITableViewDelegate>
/**
 *  车型数据
 */
@property (nonatomic, strong) NSArray *carTypeData;
/**
 *  高端车数据
 */
@property (nonatomic, strong) NSArray *highCarData;
/**
 *  最新车源数据
 */
@property (nonatomic, strong) NSArray *newsCarData;
@property(nonatomic,weak)UITableView *tableView;
@end

@implementation DYZCarSourceController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self loadCarData];
    [self setupUI];
}
/**
 *  加载高端品牌数据
 *
 *  @return <#return value description#>
 */
-(void)loadCarData{
    //加载车型数据
    NSArray *arr = @[@{@"name":@"现车",@"url":@"cy_xc"},@{@"name":@"热销车型",@"url":@"cy_rxc"},@{@"name":@"平行进口车",@"url":@"cy_jkc"}];
    NSArray *carType = [NSArray yy_modelArrayWithClass:[DYZHighCarModel class] json:arr];
    self.carTypeData = carType;
    //NSLog(@"%@",self.carTypeData);
    //加载高端车数据
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    //"account":"13800111100","pasword":"123456"
    [param setValue:@"1" forKey:@"accountid"];
    
    [DYZNetworkManager postUrlString:@"http://iosapi.itcast.cn/car/highcarlist.json.php" withParam:param withSuccessBlock:^(id data) {
        NSArray *hArr = [NSArray yy_modelArrayWithClass:[DYZHighCarModel class] json:data[@"data"][@"high"]];
        self.highCarData = hArr;
        [self.tableView reloadData];
        } withFailedBlock:^(NSError *error) {
        NSLog(@"%@",error);
    } withErrorBlock:^(NSString *message) {
        NSLog(@"%@",message);
    }];
    //加载最新车源数据
    [DYZNetworkManager postUrlString:@"http://iosapi.itcast.cn/car/newcarlist.json.php" withParam:param withSuccessBlock:^(id data) {
        //        NSLog(@"%@",data[@"data"][@"carList"]);
        NSArray *newArr = [NSArray yy_modelArrayWithClass:[DYZNewCarsModel class] json:data[@"data"][@"carList"]];
        // NSLog(@"%@",newArr);
        self.newsCarData = newArr;
        [self.tableView reloadData];
    } withFailedBlock:^(NSError *error) {
        NSLog(@"%@",error);
    } withErrorBlock:^(NSString *message) {
        NSLog(@"%@",message);
    }];
}

/**
 *  searchView
 */
-(void)setupUI{
    //
    UIButton *btn = [[UIButton alloc]init];
    [btn setImage:[UIImage imageNamed:@"add"] forState:UIControlStateNormal];
    [btn setTitle:@"发布车源" forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:14];
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithCustomView:btn];
    [btn sizeToFit];
    //添加发布车源的点击事件
    [btn addTarget:self action:@selector(launchCarsButtonClick) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = item;
    self.navigationController.navigationBar.backgroundColor = [UIColor colorWithRed:0 green:0 blue:1 alpha:1];
    //创建添加搜索视图
    DYZSearchView *searchView = [[DYZSearchView alloc]init];
    [self.view addSubview:searchView];
    //
    [searchView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.offset(0);
        make.height.offset(42);
    }];
    //创建添加tableView
    UITableView *tableView = [[UITableView alloc]init];
    self.tableView = tableView;
    [self.view addSubview:tableView];
    //添加约束
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(searchView.mas_bottom);
        make.left.right.bottom.offset(0);
    }];
    tableView.dataSource = self;
    tableView.delegate = self;
    
    [tableView registerNib:[UINib nibWithNibName:@"DYZNormalCellTableViewCell" bundle:nil] forCellReuseIdentifier:normalCellId];
    [tableView registerNib:[UINib nibWithNibName:@"DYZCarSourceCell" bundle:nil] forCellReuseIdentifier:carSourceCellId];
    //设置自动行高
    tableView.estimatedRowHeight = UITableViewAutomaticDimension;
    //tableview不被tabbar遮盖
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
}

//实现发布车源按钮的点击事件
-(void)launchCarsButtonClick{
    DYZLaunchCarsController *lc = [[DYZLaunchCarsController alloc]init];
    [self.navigationController pushViewController:lc animated:YES];
}

#pragma mark -UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section==0) {
        return self.carTypeData.count;
    }else if (section==1){
        return self.highCarData.count;
    }else{
        return self.newsCarData.count;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0||indexPath.section==1) {
        tableView.rowHeight = 60;
        DYZNormalCellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:normalCellId forIndexPath:indexPath];if (indexPath.section==0) {
            DYZHighCarModel *carTypeModel = self.carTypeData[indexPath.row];
            cell.carTypeModel = carTypeModel;
            
        }else{
            DYZHighCarModel *highModel = self.highCarData[indexPath.row];
            cell.highModel = highModel;
        }
        return cell;
    }else{
        DYZCarSourceCell *cell = [tableView dequeueReusableCellWithIdentifier:carSourceCellId forIndexPath:indexPath];
        tableView.rowHeight = 100;
        DYZNewCarsModel *newCarModel = self.newsCarData[indexPath.row];
        cell.carsModel = newCarModel;
        return cell;
    }
}
#pragma mark -UITableViewDelegate
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    //获取indexPath
    NSIndexPath *indexPath = [[NSIndexPath alloc]initWithIndex:section];
    if (indexPath.section == 0) {//第一组
        return 0;
        
    }else
        return 60;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        DYZExistingCarController *existingCarVC = [[DYZExistingCarController alloc]init];
        [self.navigationController pushViewController:existingCarVC animated:YES];
        DYZHighCarModel *carTypeModel = self.carTypeData[indexPath.row];
        existingCarVC.navigationItem.title = carTypeModel.name;
//        if (indexPath.row == 0) {
//            existingCarVC.navigationItem.title = @"现车";
//        }else if (indexPath.row == 1){
//        existingCarVC.navigationItem.title = @"热销车型";
//        }else{
//        existingCarVC.navigationItem.title = @"平行进口车";
//        }
    }
    if (indexPath.section == 1) {
        DYZCarSeriesController *carSeriesVC = [[DYZCarSeriesController alloc]init];
        [self.navigationController pushViewController:carSeriesVC animated:YES];
        DYZHighCarModel *highModel = self.highCarData[indexPath.row];
        carSeriesVC.carBrandId = highModel.carBrandId;
        carSeriesVC.navigationItem.title = highModel.name;
    }
    if (indexPath.section==2) {
        DYZCarSourceDetailController *detailVc = [[DYZCarSourceDetailController alloc]init];
        [self.navigationController pushViewController:detailVc animated:true];
        detailVc.navigationItem.title = @"车源详情";
       DYZCarSourceCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
        detailVc.carsModel = cell.carsModel;
    }
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    //设置组头视图
    UIView *view = [[UIImageView alloc]init];
    view.backgroundColor = [UIColor whiteColor];
    UIImageView *imageView = [[UIImageView alloc]init];
    [view addSubview:imageView];
    //添加lable
    UILabel *lable = [[UILabel alloc]init];
    lable.font = [UIFont systemFontOfSize:14];
    [view addSubview:lable];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(16);
        make.left.offset(14);
        make.width.height.offset(30);
    }];
    [lable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(imageView.mas_right).offset(8);
        make.top.bottom.offset(0);
    }];
    //
    NSIndexPath *indexPath = [[NSIndexPath alloc]initWithIndex:section];
    if (indexPath.section == 0) {//第一组
        return nil;
    }else if (indexPath.section == 1){
        imageView.image = [UIImage imageNamed:@"u101"];
        // view.backgroundColor = [UIColor grayColor];
        [lable setText:@"高端品牌"];
        return view;
    }else
        imageView.image = [UIImage imageNamed:@"u99"];
    //    view.backgroundColor = [UIColor blueColor];
    [lable setText:@"最新车型"];
    return view;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
